function snapsHub() {
	
}

snapsHub.prototype.setNeedle = function(newNeedle) {
	this.settings.needle = newNeedle;
	this.settings.needleRegExp = new RegExp(newNeedle,"gi");	
}

snapsHub.prototype.settings = {
	timeout:"",
	needle:"",
	needleRegExp:"",
	queryOptions:"",
	snapsPerTab:"8",
	maxSourceItems:"100",
	thumbsize:"288u",
	loadedLibs:"0",
	transitionSpeed:7000
}

snapsHub.prototype.init = function(targetDiv) {
	//Controller
	if (typeof(snapsHubController)!="object") {
		snapsHubController = {
			settings:this.settings,
			targetDiv:targetDiv,
			init:function() {
				this.fetch.add("picasa");
				this.fetch.add("flickr");
				this.fetch.init();
			},
			fetch: {
				librariesHold:new Array(),
				add:function(library) {
					this.librariesHold.push(library);
				},
				init:function() {
					snapsHubController.settings.totalLibs = this.librariesHold.length;
					for (var i=0; i < this.librariesHold.length; i++) {
						var source = this.librariesHold[i];
						source = source.toLowerCase();	
						snapsHubDB.fetch[source]();
					}
				}
			},
			allLibsLoaded:function() {
				if(snapsHubController.settings.loadedLibs == snapsHubController.settings.totalLibs) {
					snapsHubDB.library = PTPObject.shuffle(snapsHubDB.library);
					snapsHubView.singleView();	
				}
			},
			launchMoreBox:function() {
				if (($("#snapsAllView-holder").length)<1) {
					snapsHubView.allView.init(snapsHubDB.currentIndex);	
				}
				snapsHubView.allView.launchTB();	
			}
		}
	}
	
	//View
	if (typeof(snapsHubView)!="object") {
		snapsHubView = {
			singleView:function() {
				var ceiling = snapsHubDB.library.length;
				//console.log("ceiling = "+ceiling);
				var randomnumber=Math.floor(Math.random()*ceiling)
				//console.log("randomnumber = "+randomnumber);
				
				var self = snapsHubDB.library[randomnumber];
					
					var imageuri = self.thumb.uri;
					var user = '<a class="snapsLink" target="_blank" href="'+self.full+'">'+self.author.name+'</a>';
					
					$("#"+snapsHubController.targetDiv)
						.html('<img border="0" align="center" height="180" src="'+imageuri+'"/>')
						//.css('background-image','url('+imageuri+')')
						.unbind()
						.click(function() {
							clearTimeout(snapsHubController.timeout);			
							snapsHubView.singleView();
						});
					$("#snaps-credit").html("Photo by "+user+" from "+self.source);
					
					snapsHubController.timeout = setTimeout(snapsHubView.singleView,snapsHubController.settings.transitionSpeed);
			},
			allView:{
				init:function (startIndex){
					//console.log("starting snaps tb initialization");
					//Construct div with contents
					if($("#snapsAllView-holder").length<1) {
						//console.log("ceating snapsAllView-holder element");
						$("#"+snapsHubController.targetDiv).after('<div style="display:none;" id="snapsAllView-hook"><div style="height:420px; width:780px; overflow:auto;" align="center"><table border="0" cellpadding="6" cellspacing="0" height="400px;"><tr id="snapsAllView-holder"></tr></table></div></div>');
					} else {
						//$("#snapsAllView-holder").empty();	
						//console.log("emptying snapsAllView-holder element");
					}
					
					if ($("#snapsMoreCell").length>0) {
						$("#snapsMoreCell").remove();
					}
										
					//console.log("startIndex= "+startIndex);
					//loop through contents
					for (var i=startIndex; (i<snapsHubDB.library.length)&&(i<(parseInt(startIndex)+parseInt(snapsHubController.settings.snapsPerTab))); i++) {
						
						this.drawPhotoLink(i);
						
						snapsHubDB.currentIndex = i;
					}
					this.createButtons(startIndex);
				},
				drawPhotoLink: function(id) {
					var self = snapsHubDB.library[id];
					var user = '<a target="_blank" class="snapsLink" href="'+self.full+'">'+self.author.name+'</a>';
					$("#snapsAllView-holder").append('<td><table border="0" cellpadding="0" cellspacing="0"><tr><td height="300px" width="300px" valign="middle"><a target="_blank" href="'+self.full+'"><img border="0" style="border:6px solid #ffffff;" src="'+self.thumb.uri+'"/></a></td></tr><tr><td><p class="snapsCaption">Photo by '+user+' from '+self.source+'</p></td></tr></table></td>');
				},
				launchTB: function() {
					tb_show("More Pics",  "#TB_inline?height=440&width=800&inlineId=snapsAllView-hook");	
				},
				createButtons:function(startIndex) {
					//Create More Button
					if ((parseInt(startIndex)+parseInt(snapsHubController.settings.snapsPerTab))<snapsHubDB.library.length) {
						$("#snapsAllView-holder").append('<td id="snapsMoreCell"><a href="javascript:snapsHubView.allView.init('+eval(parseInt(startIndex)+parseInt(snapsHubController.settings.snapsPerTab))+');" title="See More Photos"><img border="0" src="http://www.peopletopeople.com/PTP%20Documents/globalHub/img/callToActionArrow.png"/></a></td>');
					} else {
						$("#snapsMoreCell").remove();	
					}
					
				}
			}
		}		
	}
	
	//Model
	if (typeof(snapsHubDB)!="object") {
		snapsHubDB = {
			currentIndex:0,
			library:new Array(),
			fetch:{
				picasa:function() {
					//console.log("Loading Picasa Stuff.");
					$.ajax({
						type:"GET",
						dataType:"jsonp",
						notNull:true,
						url: "http://picasaweb.google.com/data/feed/api/all?alt=json-in-script&access=public&kind=photo&thumbsize="+snapsHubController.settings.thumbsize+"&max-results="+snapsHubController.settings.maxSourceItems+"&q="+snapsHubController.settings.needle,
						error: function(XMLHttpRequest, textStatus, errorThrown){
							$('<p></p>')
							.html("There was an error with your request")
							.appendTo('#errorPane');
							returnVal = false;
						},
						success: function(html) {
							var AlbumQuery = eval(html);
							if (AlbumQuery.feed.entry) { //didn't return a zero value
								for (var i=0; i < AlbumQuery.feed.entry.length; i++) {
									var self = AlbumQuery.feed.entry[i];
									var photo = {
										caption:self.media$group.media$title.$t,
										thumb:{
											uri:self.media$group.media$thumbnail[0].url,
											width:self.media$group.media$thumbnail[0].width,
											height:self.media$group.media$thumbnail[0].height
										},
										full:self.link[1].href,
										source:"Picasa",
										author:{
											name:self.author[0].gphoto$user.$t,
											uri:self.author[0].uri.$t
										}
									}
									
									snapsHubDB.library.push(photo);
								}							
							} else {
								snapsHubController.settings.totalLibs--;
								this.notNull = false
							}
						},
						complete:function () {
							if (this.notNull) {
								snapsHubController.settings.loadedLibs++;
							}
							snapsHubController.allLibsLoaded();
							
						}
					});		
					
				},
				flickr:function() {
					//console.log("doing flickr stuff");
					$.ajax({
						type:"GET",
						dataType:"jsonp",
						notNull:true,
						url:"http://api.flickr.com/services/rest/?format=json&method=flickr.photos.search&tags="+snapsHubController.settings.needle+"&tag_mode=all&api_key=cf86d01f7ff766b79b5d9352aefd235e&safe_search=1&privacy_filter=1&media=photos&per_page="+snapsHubController.settings.maxSourceItems+"&jsoncallback=?",
						//url:"http://api.flickr.com/services/feeds/photos_public.gne?tags="+snapsHubController.settings.needle+"&format=json&per_page=40&jsoncallback=?",
						error: function(XMLHttpRequest, textStatus, errorThrown){
							$('<p></p>')
							.html("There was an error with your request")
							.appendTo('#errorPane');
							returnVal = false;
						},
						success: function(html) {
							//console.log("Server returned "+html);
							var AlbumQuery = eval(html);
							FlickrQuery = AlbumQuery;
							if (parseInt(AlbumQuery.photos.total)>0) { //didn't return a zero value
								//console.log("first validatoin passed");
								for (var i=0; i < AlbumQuery.photos.photo.length; i++) {
									var self = AlbumQuery.photos.photo[i]; 
									
									var photo = {
										caption:self.title,
										thumb:{
											uri:"http://farm"+self.farm+".static.flickr.com/"+self.server+"/"+self.id+"_"+self.secret+"_m.jpg",
											width:"",
											height:""
										},
										full:"http://www.flickr.com/photos/"+self.owner+"/"+self.id+"",
										source:"Flickr",
										author:{
											name:self.owner,
											uri:"http://www.flickr.com/photos/"+self.owner+"/"
										}
									}
									//console.log("about to push flickr photo");
									snapsHubDB.library.push(photo);
								}							
							} else {
								snapsHubController.settings.totalLibs--;
								this.notNull = false
							}
						},
						complete:function () {
							if (this.notNull) {
								snapsHubController.settings.loadedLibs++;
							}
							snapsHubController.allLibsLoaded();
							
						}
					});
					
				}
			}
		}
	}
	
	snapsHubController.init();
}